#include "drv_wifi.h"
void WIFI_SendCommand(char* cmd)
{
    while(*cmd)
    {
        USART_SendData(USART3, *cmd++);
        while(USART_GetFlagStatus(USART3, USART_FLAG_TXE) == RESET);
    }
}
void drv_wifi_usart(void)
{
	GPIO_InitTypeDef  GPIO_InitStruct;
	USART_InitTypeDef USART_InitStruct;
	NVIC_InitTypeDef  NVIC_InitStruct;

	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB,ENABLE);
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART3,ENABLE);
	
	GPIO_InitStruct.GPIO_Mode=GPIO_Mode_AF;
	GPIO_InitStruct.GPIO_OType=GPIO_OType_PP;
	GPIO_InitStruct.GPIO_Pin=GPIO_Pin_11|GPIO_Pin_10;
	GPIO_InitStruct.GPIO_PuPd=GPIO_PuPd_UP;
	GPIO_InitStruct.GPIO_Speed=GPIO_Speed_50MHz;
	GPIO_Init(GPIOB,&GPIO_InitStruct);

	GPIO_PinAFConfig(GPIOB,GPIO_PinSource10,GPIO_AF_USART3);
	GPIO_PinAFConfig(GPIOB,GPIO_PinSource11,GPIO_AF_USART3);

	USART_InitStruct.USART_HardwareFlowControl=USART_HardwareFlowControl_None;
	USART_InitStruct.USART_BaudRate=115200;
	USART_InitStruct.USART_Mode=USART_Mode_Rx|USART_Mode_Tx;
	USART_InitStruct.USART_Parity=USART_Parity_No;
	USART_InitStruct.USART_StopBits=USART_StopBits_1;
	USART_InitStruct.USART_WordLength=USART_WordLength_8b;
	USART_Init(USART3,&USART_InitStruct);

	USART_ITConfig(USART3,USART_IT_RXNE,ENABLE);
	USART_ITConfig(USART3,USART_IT_IDLE,ENABLE);

	NVIC_InitStruct.NVIC_IRQChannel=USART3_IRQn;
	NVIC_InitStruct.NVIC_IRQChannelCmd=ENABLE;
	NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority=0;
	NVIC_InitStruct.NVIC_IRQChannelSubPriority=1;
	
	NVIC_Init(&NVIC_InitStruct);

	USART_ClearFlag(USART3,USART_FLAG_TC);
	USART_Cmd(USART3,ENABLE);
}
void drv_wifi_init(void)//PB10->TX			PB11->RX
{ 
	memset(recv_wifi,0,sizeof(recv_wifi));
	
	drv_systick_ms(2000);
	
	WIFI_SendCommand("AT\r\n");

	if(wait_OK())
	{
		printf("WIFI had connect!\r\n");
	}

	WIFI_SendCommand("AT+CWMODE=2\r\n");

	if(wait_OK())
	{
		printf("AP MODE is OK!\r\n");
	}

	WIFI_SendCommand("AT+CWSAP_DEF=\"Zydsg13\",\"12345678\",5,3\r\n");

	if(wait_OK())
	{
		printf("Hot Spot has create!\r\n");
	}

	WIFI_SendCommand("AT+CIPMUX=1\r\n");

	if(wait_OK())
	{
		printf("CIPMUX is open!\r\n");
	}

	WIFI_SendCommand("AT+CIPSERVER=1,8080\r\n");
	
	if(wait_OK())
	{
		printf("Server is OK!\r\n");
	}
}
uint8_t wait_OK(void)
{
		uint16_t timeout=0;
	    memset(recv_wifi, 0, sizeof(recv_wifi));
	   while(timeout < 1000)
    {
        // 检查是否收到预期响应
        if(strstr(recv_wifi, "OK") != NULL)
        {
			return 1;
		}
		// 检查是否收到错误响应
        if(strstr(recv_wifi, "ERROR") != NULL)
        {
			printf("ERROR\r\n");
			return 0;
		}
        timeout++;
        drv_systick_ms(1);
    }
	   printf("timeout!\r\n");
	   return 0;
}
void solve_request(void)
{
		if(strstr(recv_wifi,"open") != NULL)
		{
			drv_led_open();
		}
		else if(strstr(recv_wifi,"clos") != NULL)
		{
			drv_led_close();
		}
		else if(strstr(recv_wifi,"run") != NULL)
		{
			drv_dog_run();
		}
		else if(strstr(recv_wifi,"stop") != NULL)
		{
			drv_dog_stop=1;
		}
		else if(strstr(recv_wifi,"stand") != NULL)
		{
			drv_dog_stand();
		}
		else if(strstr(recv_wifi,"lie") != NULL)
		{
			drv_dog_lie();
		}
		else if(strstr(recv_wifi,"temp") != NULL)
		{
				while(stop_temp!=1)
				{
					drv_seg( 1,tp2.left_1);
					drv_seg( 2,tp2.left_2);
					drv_seg( 3,tp2.left_3);
					drv_seg( 4,tp2.left_4);
				}
		}
		else if(strstr(recv_wifi,"temp_close") != NULL)
		{
			stop_temp=1;
		}
		memset(recv_wifi, 0, sizeof(recv_wifi));
}
